package cn.aixmy.main.service.impl;

import cn.aixmy.base.vo.Result;
import cn.aixmy.main.entity.Admin;
import cn.aixmy.main.mapper.AdminMapper;
import cn.aixmy.main.service.AdminService;
import cn.aixmy.utils.CheckUtils;
import cn.aixmy.utils.XmUtils;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.Instant;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    AdminMapper adminMapper;

    @Override
    public Result login(Admin admin) {
        Admin eqAdmin = getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUser, admin.getUser()));
        if (CheckUtils.isObjectEmpty(eqAdmin)){
            return Result.error("账号不存在！");
        }
        if (eqAdmin.getPass().equals(admin.getPass())){
            if (eqAdmin.getStatus() != 1)
                return Result.error("账号被封禁！");
            StpUtil.login(eqAdmin.getId());
            updateById(new Admin()
                    .setId(eqAdmin.getId())
                    .setLastIp(admin.getLastIp())
                    .setLastTime(XmUtils.getTimeStamp()));
            return Result.ok("登录成功！");
        }
        return Result.error("账号或密码错误！");
    }
}
